﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
using Glue.Lib;
using Glue.Lib.Servers;

namespace SqueezeboxBridge
{
    internal class InternalStreamServer : HttpServer
    {
        public InternalStreamServer(IPEndPoint localEP) : base(localEP) { }

        public override void ProcessRequest(HttpRequest request, HttpResponse response)
        {
            try
            {
                // retrieve file
                string fileid = request.RawUrl.Substring(5);
                StreamCacheFile file = StreamCache.Current.Retrieve(fileid);

                // transmitfile
                response.StatusCode = 200;
                response.ContentType = file.ContentType;
                response.Connection.SetKnownResponseHeader(HttpProtocol.HeaderContentDisposition, "attachment; filename=" + file.FileName);
                response.StatusDescription = "OK";
                response.TransmitFile(file.Path);
                response.Flush();
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                response.End();
            }
        }
    }
}
